<template>
  <div class="edit-user music-cpns-common">
    <div class="all">
      <h2>编辑个人信息</h2>
      <div class="conter">
        <div class="left">
          <div class="item">
            <span>昵称: </span>
            <el-input v-model="profile.nickname" />
          </div>
          <div class="item">
            <span>介绍: </span>
            <el-input
              v-model="profile.signature"
              :rows="2"
              type="textarea"
              maxlength="100"
              :show-word-limit="true"
              resize="none"
            />
          </div>
          <div class="item">
            <span>性别: </span>
            <el-radio-group v-model="profile.gender">
              <el-radio :label="0">保密</el-radio>
              <el-radio :label="1">男</el-radio>
              <el-radio :label="2">女</el-radio>
            </el-radio-group>
          </div>
          <div class="item">
            <span>生日: </span>
            <el-date-picker
              v-model="profile.birthday"
              type="date"
              format="YYYY/MM/DD"
              value-format="x"
            />
          </div>
          <div class="item">
            <span>地区: </span>
            <el-input v-model="profile.address" />
          </div>
          <div class="item">
            <span>状态: </span>
            <el-dropdown
              size="small"
              split-button
              type="primary"
              @command="handelCommand"
              trigger="click"
            >
              {{ onlies[currentI] }}
              <template #dropdown>
                <el-dropdown-menu>
                  <template v-for="(sp, index) in onlies" :key="sp">
                    <el-dropdown-item :command="{ value: sp, index }">{{
                      sp
                    }}</el-dropdown-item>
                  </template>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </div>
          <div class="save-canle">
            <button
              class="user-btn"
              :style="{
                background: bcColor,
                color: fontColor,
                marginRight: '10px'
              }"
              @click="handelSave"
            >
              保存
            </button>
            <button class="user-btn" @click="handelCanle">取消</button>
          </div>
        </div>
        <div class="right">
          <div class="img">
            <img :src="avarUrl" alt="404" />
          </div>
          <button class="user-btn" @click="handelAvatar">修改头像</button>
          <input
            type="file"
            name="avatar"
            accept="image/gif,image/jpeg,image/jpg,image/png"
            style="display: none"
            @change="changeImage"
            ref="avatarInput"
          />
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
import { updateUser, updateAvatar } from '@/service/edituser'
import { getUserDetail } from '@/service/user'
import { TUser } from '@/service/edituser/type'
import LocalCache from '@/utils/cache'
import { Message } from '@/utils/message'

const store = useStore()
const router = useRouter()
const avatarInput = ref<HTMLInputElement>()
const bcColor = computed(() => store.state.theme.bcColor)
const fontColor = computed(() => store.state.theme.fontColor)
const avarUrl = ref(
  'https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png'
)
//初始化操作
const uid = store.getters['login/getUserId']
const profile = ref({ ...store.state.login.profile })
const onlies = ['隐身', '请勿打扰', '离线', '在线']
const currentI = ref(onlies.length - 1)
function initDone() {
  profile.value.birthday = String(profile.value.birthday)
  const address = LocalCache.getCache('address')
  const cacheCurrentI = LocalCache.getCache('onlineCurrentI')
  if (address) {
    profile.value.address = address
  } else {
    profile.value.address = '江西省南昌市'
  }
  if (cacheCurrentI) {
    currentI.value = cacheCurrentI
  }
  avarUrl.value = profile.value.avatarUrl
}
initDone()

// 在线
const handelCommand = (item: any) => {
  currentI.value = item.index
  Message('修改成功,当前状态为:' + item.value, 'success')
  LocalCache.setCache('onlineCurrentI', currentI.value)
}

// 保存
const handelSave = () => {
  const { gender, birthday, nickname, signature, province, city, address } =
    profile.value
  LocalCache.setCache('address', address)
  const obj = {
    gender,
    birthday,
    nickname,
    signature,
    province,
    city
  }
  pullUsers(obj)
}
const pullUsers = (obj: TUser) => {
  updateUser(obj).then((res: any) => {
    if (res.code === 200) {
      Message('修改成功', 'success')
    }
    getUsers(uid)
  })
}
const getUsers = (id: number) => {
  getUserDetail(id).then((res: any) => {
    store.commit('login/saveProfile', res.profile)
    profile.value = { ...res.profile }
    initDone()
  })
}
// 取消
const handelCanle = () => {
  router.back()
}

// changeImage
const changeImage = (e: any) => {
  const file = e.target.files[0] //得到图片地址
  upAvatar(file)
}
const handelAvatar = () => {
  avatarInput.value?.click()
}
const upAvatar = (file: any) => {
  var formData = new FormData()
  formData.append('imgFile', file)
  updateAvatar(formData).then((res: any) => {
    if (res.code === 200) {
      Message('修改成功', 'success')
    }
    avarUrl.value = res.data.url
    profile.value.avatarUrl = res.data.url
    store.commit('login/saveProfile', profile.value)
  })
}
</script>

<style scoped lang="less">
.edit-user {
  width: 100%;
  display: flex;
  .all {
    width: 70%;
    border-radius: 10px;
    border: 2px solid #ebebeb;
    box-shadow: rgba(100, 100, 111, 0.2) 0px 7px 29px 0px;
    padding: 15px 20px;
    margin-left: 30px;
    margin-top: 40px;
    padding-bottom: 90px;
  }
  h2 {
    text-align: start;
    color: #333;
    margin: 5px 0px 10px;
  }
  .conter {
    display: flex;
    .left {
      margin-right: 40px;
      width: 520px;
      > div {
        margin-bottom: 10px;
        &.item {
          display: flex;
          align-items: center;
        }
        span {
          margin-right: 10px;
          display: inline-block;
          width: 50px;
        }
        &.save-canle {
          text-align: center;
        }
      }
    }
    .right {
      text-align: center;
      .img img {
        width: 155px;
        height: 155px;
        border: 1px solid #eee;
      }
    }
    button.user-btn {
      background: #fff;
      color: #333;
      border: 1px solid #ebebeb;
      padding: 6px 20px;
      font-size: 12px;
      border-radius: 20px;
      cursor: pointer;
      margin-top: 10px;
    }
  }
}
</style>
